package com.agileai.bpm.controller.define;

import java.util.List;

import com.agileai.bpm.interceptor.BaseInterceptor;
import com.agileai.domain.DataParam;
import com.agileai.hotweb.controller.core.BaseHandler;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.renders.DispatchRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.StringUtil;

public class WfInterceptorTypeSelectHandler extends BaseHandler {
	public ViewRenderer prepareDisplay(DataParam param){
		this.mergeParam(param);
		this.setAttributes(param);
		this.initAttriubtes(param);
		return new LocalRenderer(getPage());
	}
	
	@SuppressWarnings("rawtypes")
	public ViewRenderer doContinueAction(DataParam param){
		this.storeParam(param);
		String interceptorType = param.get("interceptorType");
		Class handlerClass = getBackHandlerClass(interceptorType);
		return new DispatchRenderer(getHandlerURL(handlerClass));
	}
	
	@SuppressWarnings("rawtypes")
	public static Class getBackHandlerClass(String interceptorType){
		Class result = null;
		if (BaseInterceptor.Type.DBOperater.equals(interceptorType)){
			result = WfDBOperatorEditHandler.class;
		}
		else if (BaseInterceptor.Type.RestInvoker.equals(interceptorType)){
			result = WfWebServiceInvokerEditHandler.class;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	private void initAttriubtes(DataParam param){
		FormSelect formSelect = new FormSelect();
		formSelect.setHasBlankValue(false);
		List types = BaseInterceptor.Type.getTypeDataSet();
		formSelect.putValues(types);
		this.setAttribute("interceptorType", formSelect);
		String interceptorType = param.get("interceptorType");
		formSelect.setSelectedValue(interceptorType);
		String interceptorName = param.get("interceptorName");
		setAttribute("interceptorName", "数据库操作X");
		if ((!StringUtil.isNullOrEmpty(interceptorType)) && StringUtil.isNullOrEmpty(interceptorName)){
			String tempInterceptorName = BaseInterceptor.getInterceptorTypeName(interceptorType);
			setAttribute("interceptorName", tempInterceptorName);
		}
	}
}
